from rest_framework.decorators import api_view
from rest_framework.response import Response
from student.models import StudentData

@api_view(["POST"])
def batch_create(request):
    file = request.FILES["student_info"]
    type_excel = file.name.split('.')[1]
    if type_excel not in {'xlsx','xls'}:
        return Response({
            "result":False,
            "code":401,
            "message":"文件格式错误(仅支持xls、xlsx)"
        })
    all_count,fails,suc_cou,fail_cou = StudentData.objects.save_excel_data(file)
    return Response({
        "result":True,
        "code":0,
        "message":"导入成功",
        "data":{
            "all_cou":all_count,
            "success_cou":suc_cou,
            "fail_cou":fail_cou,
            "fail_rows":",".join(fails)
        }
    })
